package com.ruoyi.project.para.srn.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.oms.paeaAuType.service.IPaeaAuTypeService;
import com.ruoyi.project.para.srn.domain.SmSrnInfo;
import com.ruoyi.project.para.srn.service.ISmSrnInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 核算码Controller
 *
 * @author ruoyi
 * @date 2020-03-14
 */
@Controller
@RequestMapping("/para/srn")
public class SmSrnInfoController extends BaseController
{
    private String prefix = "para/srn";

    @Autowired
    private ISmSrnInfoService smSrnInfoService;

    @Autowired
    private IPaeaAuTypeService paeaAuTypeService;

    @RequiresPermissions("para:srn:view")
    @GetMapping()
    public String srn()
    {
        return prefix + "/srn";
    }

    /**
     * 查询核算码列表
     */
    @RequiresPermissions("para:srn:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SmSrnInfo smSrnInfo)
    {
        startPage();
        List<SmSrnInfo> list = smSrnInfoService.list(new QueryWrapper<SmSrnInfo>(smSrnInfo));
        return getDataTable(list);
    }

    /**
     * 导出核算码列表
     */
    @RequiresPermissions("para:srn:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SmSrnInfo smSrnInfo)
    {
        List<SmSrnInfo> list = smSrnInfoService.list(new QueryWrapper<SmSrnInfo>(smSrnInfo));
        ExcelUtil<SmSrnInfo> util = new ExcelUtil<SmSrnInfo>(SmSrnInfo.class);
        return util.exportExcel(list, "srn");
    }

    /**
     * 新增核算码
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("auTypes", paeaAuTypeService.selectPaeaAuTypeAll());
        return prefix + "/add";
    }

    /**
     * 新增保存核算码
     */
    @RequiresPermissions("para:srn:add")
    @Log(title = "核算码", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SmSrnInfo smSrnInfo)
    {
        return toAjax(smSrnInfoService.saveOrUpdate(smSrnInfo));
    }

    /**
     * 修改核算码
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("auTypes", paeaAuTypeService.selectPaeaAuTypesBySrnId(id));
        SmSrnInfo smSrnInfo = smSrnInfoService.getById(id);
        mmap.put("smSrnInfo", smSrnInfo);
        return prefix + "/edit";
    }

    /**
     * 修改保存核算码
     */
    @RequiresPermissions("para:srn:edit")
    @Log(title = "核算码", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SmSrnInfo smSrnInfo)
    {
        return toAjax(smSrnInfoService.saveOrUpdate(smSrnInfo));
    }

    /**
     * 删除核算码
     */
    @RequiresPermissions("para:srn:remove")
    @Log(title = "核算码", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(smSrnInfoService.removeByIds(Arrays.asList(ids.split(","))));
    }
}
